package com.loong.musicdemo.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.ui.find.activity.ShowAlbumPlayListActivity;

import java.util.ArrayList;
import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.ViewHolder> {
    private final List<SongList> myPlayList;//歌单信息
    private RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.temp);

    public PlayListAdapter(List<SongList> myPlayList) {
        this.myPlayList = myPlayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_my_play_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SongList songList = myPlayList.get(position);
        holder.name.setText(songList.getName());
        holder.nickName.setText("by:" + songList.getCreatorName());
        holder.count.setText("共" + songList.getPlayCount() + "次播放");
        Glide.with(holder.coverImg.getContext())
                .load(songList.getBackgroundUrl())
                .apply(requestOptions)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(holder.coverImg);
        holder.view.setOnClickListener(v -> {
            if (songList.getSongListId() != null) {
                Intent songIntent = new Intent(holder.view.getContext(), ShowAlbumPlayListActivity.class);
                songIntent.putExtra("albumName", songList.getName());
                songIntent.putExtra("albumImg", songList.getBackgroundUrl());
                songIntent.putExtra("albumIDs", (ArrayList) songList.getSongIds());
                songIntent.putExtra("albumCreatorName", songList.getCreatorName());
                songIntent.putExtra("albumCreatorPic", songList.getCreatorAvatarUrl());
                songIntent.putExtra("albumCommentCount", songList.getCommentCount());
                songIntent.putExtra("albumPlayCount", songList.getPlayCount());
                holder.view.getContext().startActivity(songIntent);
            } else {//歌单信息未被获取
                Toast.makeText(holder.view.getContext(), "获取歌单信息中", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return myPlayList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View view;//当期视图
        ImageView coverImg;//歌单封面图片
        TextView name;//歌单名
        TextView nickName;//歌单主昵称
        TextView count;//歌单歌曲数目

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            coverImg = itemView.findViewById(R.id.coverImg);
            name = itemView.findViewById(R.id.name);
            nickName = itemView.findViewById(R.id.nickname);
            count = itemView.findViewById(R.id.count);
        }
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        //释放图片资源
        Glide.with(holder.view.getContext()).clear(holder.coverImg);//清除资源
    }
}
